<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 使用 WebWork 开辟多线程,解决前端越来越复杂的问题：大数据渲染、3d可视化、数据压缩
        var worker = new Worker('./worker.js');
        worker.postMessage('Hello world'); 
        worker.onmessage = function(event) {
            console.log('Received message' + event.data);
        }

        const file = new File(["a".repeat(1000000)],'test.txt');

        const chunkSize = 40000;
        const url = 'http://httpbin.org/post';

        async function chunkedUpload() {

            console.log('---------');
            for (let start = 0;start < file.size;start += chunkSize) {
                const chunk = file.slice(start,start+chunkSize);

                const fd = new FormData(); // 表单对象
                fd.append('data',chunk)
                await fetch(url,{method:'POST',body:fd}).then(res => {
                    return res.json()
                }).then(data => {
                    console.log(data);
                })

            }

        }

        chunkedUpload()

    </script>

</body>
</html>